/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Menu.java
 *
 * Created on 2011.11.12., 18:24:20
 */

package eletjatek;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ZoZo
 */
public class Menu extends javax.swing.JFrame {

    /** Creates new form Menu */
    public Menu() {
        initComponents();
        this.setLocationRelativeTo(null);
        meret.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        meret = new javax.swing.JDialog();
        x = new javax.swing.JTextField();
        y = new javax.swing.JTextField();
        ok = new javax.swing.JButton();
        xLabel = new javax.swing.JLabel();
        yLabel = new javax.swing.JLabel();
        startB = new javax.swing.JButton();
        image = new javax.swing.JLabel();
        menuBar = new javax.swing.JMenuBar();
        file = new javax.swing.JMenu();
        startM = new javax.swing.JMenuItem();
        exit = new javax.swing.JMenuItem();
        help = new javax.swing.JMenu();
        uManual = new javax.swing.JMenuItem();

        meret.setTitle("Táblaméret");
        meret.setMinimumSize(new java.awt.Dimension(152, 117));
        meret.setModal(true);
        meret.setName(""); // NOI18N
        meret.setResizable(false);

        x.setText("0");
        x.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                xFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                xFocusLost(evt);
            }
        });

        y.setText("0");
        y.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                yFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                yFocusLost(evt);
            }
        });

        ok.setText("Ok");
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });

        xLabel.setText(":x");

        yLabel.setText(":y");

        javax.swing.GroupLayout meretLayout = new javax.swing.GroupLayout(meret.getContentPane());
        meret.getContentPane().setLayout(meretLayout);
        meretLayout.setHorizontalGroup(
            meretLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(meretLayout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(meretLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(x, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(y, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(meretLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(meretLayout.createSequentialGroup()
                        .addComponent(yLabel)
                        .addGap(18, 18, 18)
                        .addComponent(ok))
                    .addComponent(xLabel))
                .addGap(23, 23, 23))
        );
        meretLayout.setVerticalGroup(
            meretLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(meretLayout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(meretLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(x, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(xLabel))
                .addGap(18, 18, 18)
                .addGroup(meretLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(y, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(yLabel)
                    .addComponent(ok))
                .addContainerGap(37, Short.MAX_VALUE))
        );

        meret.getAccessibleContext().setAccessibleName("Table Size");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Életjáték");
        setResizable(false);

        startB.setText("Start");
        startB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startBActionPerformed(evt);
            }
        });

        image.setIcon(new javax.swing.ImageIcon(getClass().getResource("/eletjatek/files/startimage.jpg"))); // NOI18N

        file.setText("File");

        startM.setText("Start");
        startM.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startMActionPerformed(evt);
            }
        });
        file.add(startM);

        exit.setText("Exit");
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });
        file.add(exit);

        menuBar.add(file);

        help.setText("Help");

        uManual.setText("UserManual");
        uManual.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                uManualActionPerformed(evt);
            }
        });
        help.add(uManual);

        menuBar.add(help);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(image)
            .addGroup(layout.createSequentialGroup()
                .addGap(172, 172, 172)
                .addComponent(startB))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(image)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(startB)
                .addContainerGap())
        );

        getAccessibleContext().setAccessibleName("The Game of Life");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
        System.exit(0);                 //kilépés a programból
    }//GEN-LAST:event_exitActionPerformed

    private void startBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startBActionPerformed
        meret.setVisible(true);         //megnyitja a méretválasztó dialógot
    }//GEN-LAST:event_startBActionPerformed

    private void startMActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startMActionPerformed
        meret.setVisible(true);         //megnyitja a méretválasztó dialógot
    }//GEN-LAST:event_startMActionPerformed

    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
        meret.setVisible(false);

        try{
            int sx=Integer.parseInt(x.getText());        //ha nem lett megadva érték,    
            int sy=Integer.parseInt(y.getText());        //vagy éppen 0 volt a kezdőképernyővel 
            if(sx==0) sx=41;                             //megegyező méretű tábla fog létrejönni
            if(sy==0) sy=26;
            GameOfLife game=new GameOfLife(sx,sy,this);
            game.setVisible(true);
            this.setVisible(false);
        } catch(NumberFormatException e){
            Fault error=new Fault("Rossz érték 'X' vagy 'Y' koordináta helyén!");
            error.setVisible(true);
        }
        x.setText("0");
        y.setText("0");
    }//GEN-LAST:event_okActionPerformed

    private void uManualActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_uManualActionPerformed
        //megnyitja az elkészített usermanual-t a megfelelő programmal
        File f=new File(System.getProperty("user.dir")+"/src/eletjatek/files/UserManual.pdf");
        try {
            Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + f.getAbsolutePath());
        } catch (IOException ex) {
            Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);
            Fault error=new Fault("Hibás vagy megszakított I/O művelet!");
            error.setVisible(true);
        }
    }//GEN-LAST:event_uManualActionPerformed

    private void xFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_xFocusGained
        if(x.getText().equals("0")) x.setText("");      
    }//GEN-LAST:event_xFocusGained

    private void yFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_yFocusGained
        if(y.getText().equals("0")) y.setText("");
    }//GEN-LAST:event_yFocusGained

    private void xFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_xFocusLost
        Dimension dm=Toolkit.getDefaultToolkit().getScreenSize();
        try{
            if(x.getText().equals("") || Integer.parseInt(x.getText())*10>dm.width-1 || Integer.parseInt(x.getText())<0){
                //Fault error=new Fault("Rossz érték 'X' koordináta helyén!");
                x.setText("0");
            }
        } catch(NumberFormatException e){
            Fault error=new Fault("Rossz érték 'X' koordináta helyén!");
            error.setVisible(true);
            x.setText("0");
        }
    }//GEN-LAST:event_xFocusLost

    private void yFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_yFocusLost
        Dimension dm=Toolkit.getDefaultToolkit().getScreenSize();
        try{
            if(y.getText().equals("") || Integer.parseInt(y.getText())*10>dm.height-150 || Integer.parseInt(y.getText())<0){
                //Fault error=new Fault("Rossz érték 'Y' koordináta helyén!");
                y.setText("0");
            }
        } catch(NumberFormatException e){
            Fault error=new Fault("Rossz érték 'Y' koordináta helyén!");
            error.setVisible(true);
            y.setText("0");
        }
    }//GEN-LAST:event_yFocusLost

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Menu().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem exit;
    private javax.swing.JMenu file;
    private javax.swing.JMenu help;
    private javax.swing.JLabel image;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JDialog meret;
    private javax.swing.JButton ok;
    private javax.swing.JButton startB;
    private javax.swing.JMenuItem startM;
    private javax.swing.JMenuItem uManual;
    private javax.swing.JTextField x;
    private javax.swing.JLabel xLabel;
    private javax.swing.JTextField y;
    private javax.swing.JLabel yLabel;
    // End of variables declaration//GEN-END:variables

}
